package cn.linran.org.service.impl;

import cn.linran.basic.service.impl.BaseServiceImpl;
import cn.linran.org.domain.Employee;
import cn.linran.org.mapper.EmployeeMapper;
import cn.linran.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService{
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 重写保存
     * @param employee
     */
    @Override
    public void save(Employee employee) {
        // 保存employee表
        employeeMapper.save(employee);  // 返回自增的主键

        // 保存中间表
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRole_id());
    }

    /**
     * 重写更新
     * @param employee
     */
    @Override
    public void update(Employee employee) {

        super.update(employee);

        // 根据id删除t_employee_role
        employeeMapper.removeEmployeeRole(employee.getId());

        // 再加一波
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRole_id());

    }

    /**
     * 重写删除
     * @param eid
     */
    @Override
    public void remove(Long eid) {
        // 先删除t_employee_role
        employeeMapper.removeEmployeeRole(eid);

        employeeMapper.remove(eid);

    }

    @Override
    public List<Employee> loadEmpByShopId(Long id) {
        System.out.println(employeeMapper.loadEmpByShopId(id));
        return employeeMapper.loadEmpByShopId(id);
    }
}
